import 'package:flutter_benchu/utils/commonPkg.dart';
import 'package:device_info/device_info.dart';

// 获取国际化文字
String text(BuildContext context, String key) {
  return IntlUtil.getString(context, key);
}

// 所有的 Toast操作
showToast(String text, {ToastIconType iconType = ToastIconType.none}) {
  Fluttertoast.showToast(
    msg: text ?? '',
    toastLength: Toast.LENGTH_SHORT,
    gravity: ToastGravity.CENTER,
    fontSize: 16,
    textColor: Colors.white,
    backgroundColor: const Color.fromRGBO(0, 0, 0, 0.8),
  );
}

//拆解参数进行拼接并进行uri编码
String resolveParam(Map<String, dynamic> param, String rowPath) {
  String path = rowPath;
  List<String> allkeys = param.keys.toList();
  List<String> params = [];
  for (String key in allkeys) {
    final encoded = Uri.encodeComponent(param[key].toString());
    params.add('$key=$encoded');
  }
  if (allkeys.isNotEmpty) {
    path += '?';
    path += params.join('&');
  }
  return path;
}

//判断res是否有错误
apiIsError(Map<String, dynamic> res) {
  bool iserr = true;
  int code = res['code'] ?? 888001;
  List<int> errList = [160024, 160025, 160026, 160027];
  if (code == 200) iserr = false;
  if (errList.indexOf(code) != -1) iserr = true;
  return iserr;
}


//关闭app
closeApp() async {
  // 退出app
  await SystemChannels.platform.invokeMethod('SystemNavigator.pop');
}

//获取权限
Future<bool> checkPermission(PermissionGroup permissionGroup) async {
  if (PermissionStatus.granted ==
      await PermissionHandler().checkPermissionStatus(permissionGroup)) {
    return true;
  } else {
    Map<PermissionGroup, PermissionStatus> permissions =
        await PermissionHandler().requestPermissions([permissionGroup]);
    if (PermissionStatus.granted == permissions[permissionGroup]) {
      return true;
    } else {
      return false;
    }
  }
}

//获取应用版本号
Future<String> getAppVersion() async {
  PackageInfo packageInfo = await PackageInfo.fromPlatform();
  return packageInfo.version;
}

//获取设备uuid （用来做加密以及scoket）
Future<String> getDeviceUUID() async {
  DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
  if (Platform.isIOS) {
    IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
    return iosInfo.identifierForVendor;
  } else if (Platform.isAndroid) {
    AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
    return androidInfo.fingerprint;
  }
  return null;
}